//
//  HistoryVC.m
//  UHealth
//
//  Created by gjtxz on 14/12/29.
//  Copyright (c) 2014年 gjtx. All rights reserved.
//

#import "HistoryVC.h"
#import "HistoryCell.h"
#import "MJRefresh.h"
#import "DBVC.h"
#import "DBManage.h"
#import "NSString+BloodSugar.h"
#import "DataDetailViewController.h"
#import "AFNetworking.h"

#import "DBManage.h"

#import "MBProgressHUD+YCL.h"

@interface HistoryVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)  UITableView        *historyTable;
/** 本地历史数据 */
@property (nonatomic, strong)  NSMutableArray     *historyArray;
/** 云端历史数据 */
@property (nonatomic, strong) NSMutableArray      *yunHistoryArray;


/** 列表日期数组 */
@property (nonatomic, strong) NSMutableArray      *dateArray;

@property (nonatomic)          NSInteger          showType;

@end

@implementation HistoryVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = kBacColor;
        //        self.title = kLocal(@"Histoty");
        self.title = @"历史";
        self.tabBarItem.image = [UIImage imageNamed:@"history_grey"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"history_blue"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self initData];
    
    // 先下拉刷新一次
    [self.historyTable headerBeginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) initView
{
    NSArray *segArray = [[NSArray alloc]initWithObjects:kLocal(@"Local Data"),kLocal(@"Cloud Data"),nil];
    UISegmentedControl *histotySeg = [[UISegmentedControl alloc]initWithItems:segArray];
    histotySeg.frame = CGRectMake(40, 25, self.view.frame.size.width-80, 30);
    histotySeg.selectedSegmentIndex = 0;
    histotySeg.backgroundColor = [UIColor clearColor];
    histotySeg.tintColor = [UIColor colorWithRed:46/255.0f green:166/255.0f blue:222/255.0f alpha:1.0];
    [histotySeg setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    [histotySeg setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
    [histotySeg addTarget:self action:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];  //添加委托方法
    
    self.navigationItem.titleView = histotySeg;
    
    // 数组初始化
    _historyArray = [NSMutableArray array];
    _yunHistoryArray = [NSMutableArray array];
    _dateArray = [NSMutableArray array];
    
    
    // 列表视图
    _historyTable = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _historyTable.dataSource =self;
    _historyTable.delegate = self;
    
    //  设置下拉刷新任务
    __weak typeof(self) weakSelf = self;
    [_historyTable addHeaderWithCallback:^{
        
        if (weakSelf.showType == 0) {
            // 加载本地历史数据
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
                [weakSelf loadHistoryData];
            }];
            blockOp.completionBlock = ^{
                [weakSelf.historyTable headerEndRefreshing];
                [weakSelf.historyTable reloadData];
            };
            [queue addOperation:blockOp];
        }
        else {
            // 获取云端历史数据日期列表
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{

                [weakSelf getDateArrayOfHistory];
                
            }];
            blockOp.completionBlock = ^{
                [weakSelf.historyTable headerEndRefreshing];
                [weakSelf.historyTable reloadData];
            };
            [queue addOperation:blockOp];
        }
        
        
        
    }];
    
    [self.view addSubview:_historyTable];
    
    // 注册 cell
    [_historyTable registerNib:[UINib nibWithNibName:NSStringFromClass([HistoryCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([HistoryCell class])];
}


#pragma mark -
#pragma mark 本地数据处理
// 加载本地历史数据
- (void)loadHistoryData {
    
    // 所有未上传的温度数据
    NSString *condition = [NSString stringWithFormat:@"%@ = 0", kIsUploaded];
    NSArray *dataArray = [[DBManage sharedManager] queryDataFromTable:kTemperatureTable andConditions:nil];
    
    if (dataArray && dataArray.count > 0) { // 有数据
        [self.historyArray removeAllObjects]; // 清空之前的数据
        
        NSString *measureIdentifierStr = ((BodyTemperatureObj *)dataArray[0]).measureIdentifier;
        NSMutableArray *tempArray = [NSMutableArray array];
        for (int i = 0; i < dataArray.count; i++) {
            if (i != dataArray.count-1) {
                BodyTemperatureObj *temperatureObj = dataArray[i];
                if ([temperatureObj.measureIdentifier isEqualToString:measureIdentifierStr]) {
                    // 取出相同测量标识的对象
                    [tempArray addObject:temperatureObj];
                }
                else {
                    measureIdentifierStr = temperatureObj.measureIdentifier;
                    NSArray *newTempArray = [tempArray sortedArrayUsingComparator:^NSComparisonResult(BodyTemperatureObj *obj1, BodyTemperatureObj *obj2) {
                        return [obj1.measureDate compare:obj2.measureDate];
                    }];
                    [self.historyArray addObject:newTempArray];
                    [tempArray removeAllObjects];
                    [tempArray addObject:temperatureObj];
                }
            }
            else {
                BodyTemperatureObj *temperatureObj = dataArray[i];
                if ([temperatureObj.measureIdentifier isEqualToString:measureIdentifierStr]) {
                    [tempArray addObject:temperatureObj];
                    NSArray *newTempArray = [tempArray sortedArrayUsingComparator:^NSComparisonResult(BodyTemperatureObj *obj1, BodyTemperatureObj *obj2) {
                        return [obj1.measureDate compare:obj2.measureDate];
                    }];
                    [self.historyArray addObject:newTempArray];
                    [tempArray removeAllObjects];
                }
                else {
                    measureIdentifierStr = temperatureObj.measureIdentifier;
                    NSArray *newTempArray = [tempArray sortedArrayUsingComparator:^NSComparisonResult(BodyTemperatureObj *obj1, BodyTemperatureObj *obj2) {
                        return [obj1.measureDate compare:obj2.measureDate];
                    }];
                    [self.historyArray addObject:newTempArray];
                    [tempArray removeAllObjects];
                }
            }
            
        }
    }
    
    // 结束刷新
    if (self.historyArray.count > 0) {
        [self.historyTable reloadData];
    }
}

// 上传温度数据
- (void)uploadData:(NSArray *)array {
    
    // 1. 模型数组 --> 字典数组
    NSMutableArray *dictArray = [NSMutableArray array];
    for (int i = 0 ; i < array.count; i++) {
        BodyTemperatureObj *b = array[i];
        
        [dictArray addObject:@{@"id"             : [NSString stringWithFormat:@"%ld", b.bodyTemperatureId],
                               @"userid"         : [NSString stringWithFormat:@"%d",(int)kNetID],
                               @"famid"          : @"0",
                               @"tw_type"        : @"1",
                               @"temperature"    : [NSString stringWithFormat:@"%.1f", b.bodyTemperatureValue],
                               @"record_time"    : [NSString stringFromDate:b.measureDate]}];
    }
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictArray
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] != 0 && error == nil){
        
        // 转换成Json字符串
        NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                     encoding:NSUTF8StringEncoding];
        
        NSString *app_Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];// app版本
        
        // 上传
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer.timeoutInterval = 10;
        manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
        NSString *postStr = [NSString stringWithFormat:@"%@/action/json/temperature.jsp?",kBaseUrl];
        NSDictionary *parameters = @{@"action"      : @"SetTWuserData",
                                     @"userid"      : [NSString stringWithFormat:@"%ld",(long)kNetID],
                                     @"famid"       : [NSString stringWithFormat:@"%ld",[[DBManage sharedManager] getDefUser].userID],
                                     @"twdata"      : jsonString,
                                     @"devid"       : @"1",
                                     @"mobile_type" : @"2",
                                     @"app_line"    : app_Version,
                                     @"qudao"       : @"Apple Store",};
        
        [manager POST:postStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            kDebug(@"Success: %@", operation.responseString);
            BOOL type = [[responseObject objectForKey:@"type"] boolValue];
            if(type){//成功
                
                // 取出本次测量ID
                BodyTemperatureObj *firstObj = [array firstObject];
                NSString *measureID = firstObj.measureIdentifier;
                
                // 体温表更新
                [[DBManage sharedManager] updateData:[NSString stringWithFormat:@"%@ = 1",kIsUploaded] andTable:kTemperatureTable andConditions:[NSString stringWithFormat:@"%@ = %@",kMeasureIdentifier, measureID]];
                
//                [self.historyArray removeObject:array];
                [_historyTable reloadData];
            }else{
            }
            
            [[iToast makeText:[responseObject objectForKey:@"msg"]] show];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            [[iToast makeText:kLocal(@"netError")] show];
        }];
        
    }else{
        
        [[iToast makeText:@"数据处理错误"] show];
    }
}


#pragma mark -
#pragma mark 获取日期列表
// 获取云端历史数据所有日期列表
- (void)getDateArrayOfHistory {
    // 获取体温时间段列表
     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
    
    NSString *postStr = [NSString stringWithFormat:@"%@/action/json/temperature.jsp?",kBaseUrl];
    NSDictionary *parameters = @{@"action"      : @"GetTWuserDataList",
                                 @"userid"      : [NSString stringWithFormat:@"%d",(int)kNetID],
                                 @"famid"       : @"0",
                                 @"pages"       : @"0"};
    
    [manager POST:postStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //          kDebug(@"请求成功: %@", responseObject);
        if ([[responseObject objectForKey:@"type"] boolValue]) {
            
            // 创建一个队列，
            NSOperationQueue *getDatesQueue = [[NSOperationQueue alloc] init];
            NSBlockOperation *opn = [NSBlockOperation blockOperationWithBlock:^{
                [self getDateArrayOfHistoryWithPagesize:[responseObject[@"pagesize"] intValue]];
            }];
            opn.completionBlock = ^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.historyTable reloadData];
                    kDebug(@"获取日期列表成功，日期数: %d", (int)self.dateArray.count);
                });
            };
            [getDatesQueue addOperation:opn];
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //
        kDebug(@"请求错误: %@", error.localizedDescription);
    }];
}

// 获取指定页的日期列表
- (void)getDateArrayOfHistoryWithPagesize:(int)pagesize {
    
    // 循环请求，修改日期列表
    for (int i = 0; i < pagesize; i++) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer.timeoutInterval = 10;
        manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
        
        NSString *postStr = [NSString stringWithFormat:@"%@/action/json/temperature.jsp?",kBaseUrl];
        NSDictionary *parameters = @{@"action"      : @"GetTWuserDataList",
                                     @"userid"      : [NSString stringWithFormat:@"%d",(int)kNetID],
                                     @"famid"       : @"0",
                                     @"pages"       : @(i)};
        
        [manager POST:postStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            BOOL type = [[responseObject objectForKey:@"type"] boolValue];
            
            if (type) {
                //                kDebug(@"请求第%d页数据成功", i);
                NSArray *jsonarray = responseObject[@"jsonarray"];
                
                for (NSDictionary *json in jsonarray) {
                    // 获取日期列表
                    if (![self.dateArray containsObject:json[@"record_time"]]) {
                        [self.dateArray addObject:json[@"record_time"]];
                    }
                    
//                    // 获取记录数据
//                    NSArray *objArray = [self jsonarrayToObjArray:jsonarray];
//                    for (BodyTemperatureObj *obj in objArray) {
//                        [[DBManage sharedManager] insertData:obj andTable:kYunTemperatureTable];
//                    }
                }
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //
            kDebug(@"请求第%d页数据失败: %@", i, error.localizedDescription);
        }];
    }
}


#pragma mark -
#pragma mark 下载温度数据
//获取指定日期的所有温度数据
- (void)downloadHistoryDataWithDate:(NSString *)dateString {
    // 1. 发送一次请求，获取数据页数
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
    
    NSString *postStr = [NSString stringWithFormat:@"%@/action/json/temperature.jsp?",kBaseUrl];
    NSDictionary *parameters = @{@"action"      : @"GetTWUserData",
                                 @"userid"      : [NSString stringWithFormat:@"%d",(int)kNetID],
                                 @"famid"       : @"0",
                                 @"record_time" : dateString,
                                 @"pages"       : @"0"};
    
    [manager POST:postStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        // 2. 请求成功后，下载所有数据
        if ([[responseObject objectForKey:@"type"] boolValue]) {
            int pagesize = [(NSString *)responseObject[@"pagesize"] intValue];
            for (int i = 0; i < pagesize; i++) {
                [self downloadHistoryDataWithDate:dateString page:i];
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //
        kDebug(@"请求某天温度数据失败: %@", error.localizedDescription);
    }];
}

// 获取指定日期、指定页码的温度数据
- (void)downloadHistoryDataWithDate:(NSString *)dateString page:(int)page {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
    
    NSString *postStr = [NSString stringWithFormat:@"%@/action/json/temperature.jsp?",kBaseUrl];
    NSDictionary *parameters = @{@"action"      : @"GetTWUserData",
                                 @"userid"      : [NSString stringWithFormat:@"%d",(int)kNetID],
                                 @"famid"       : @"0",
                                 @"record_time" : dateString,
                                 @"pages"       : @(page)};
    
    [manager POST:postStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([[responseObject objectForKey:@"type"] boolValue]) {
            // 保存历史数据
            [self.yunHistoryArray addObject:[self jsonarrayToObjArray:responseObject[@"jsonarray"]]];
            
            // 更新数据库
            [self insertYunHistoryDataWithJsonArray:responseObject[@"jsonarray"]];
        } else {
            kDebug(@"下载 %@ 第 %d 页数据失败 - %@", dateString, page, responseObject[@"msg"]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        kDebug(@"下载 %@ 第 %d 页数据失败 - %@", dateString, page, error);
    }];
}

// 云端Json数组 => 模型数组
- (NSArray *)jsonarrayToObjArray:(NSArray *)jsonarray {
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *json in jsonarray) {
        BodyTemperatureObj *obj     = [[BodyTemperatureObj alloc] init];
        obj.bodyTemperatureId       = (long)[json[@"id"] longLongValue];
        obj.bodyTemperatureValue    = [json[@"temperature"] floatValue];
        obj.measureDate             = [NSDate dateFromString:json[@"create_time"]];
        obj.userId                  = [json[@"userid"] longValue];
        [arrayM addObject:obj];
    }
    return [arrayM copy];
}

- (void)insertYunHistoryDataWithJsonArray:(NSArray *)jsonArray {
    NSArray *objArray = [self jsonarrayToObjArray:jsonArray];
    for (BodyTemperatureObj *obj in objArray) {
        obj.isDownloaded = 1; // 标记已经下载
        [[DBManage sharedManager] insertData:obj andTable:kYunTemperatureTable];
    }
}

#pragma mark -
#pragma mark 切换本地/云端
-(void)segmentAction:(UISegmentedControl *)Seg{
    
    NSInteger Index = Seg.selectedSegmentIndex;
    self.showType = Index;
    [self.historyTable headerBeginRefreshing];
    [_historyTable reloadData];
}


- (void)initData
{
    
}

#pragma mark -
#pragma mark UITableViewDelegate && UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.showType == 0) {
        return self.historyArray.count;
    } else {
        return self.dateArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __block HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HistoryCell class]) forIndexPath:indexPath];
    
    
    
    // 按钮文字
    if (self.showType == 0) {
        [cell.postBtn setTitle:@"上传数据" forState:UIControlStateNormal];
        [cell.postBtn setTitle:@"已经上传" forState:UIControlStateDisabled];
    } else {
        [cell.postBtn setTitle:@"下载数据" forState:UIControlStateNormal];
        [cell.postBtn setTitle:@"已经下载" forState:UIControlStateDisabled];
    }
    
    NSString *imageName = @"default_user";
    NSString *title = nil;
    NSString *subTitle = nil;
    if (self.showType == 0) {
        // 用户名 - 测量ID
        BodyTemperatureObj *firstT = [self.historyArray[indexPath.row] firstObject];
        title = [NSString stringWithFormat:@"%ld - %@", firstT.userId, [NSString stringFromDate:firstT.measureDate]];
        // 测试时间段
        BodyTemperatureObj *lastT = [self.historyArray[indexPath.row] lastObject];
        subTitle = [NSString stringWithFormat:@"%@ - %@", [NSString stringFromDate:firstT.measureDate], [NSString stringFromDate:lastT.measureDate]];
        
        // 根据上传状态决定显示状态
        cell.postBtn.enabled = !firstT.isUploaded;
        
    } else {
        title = [NSString stringWithFormat:@"%d",(int)kNetID];
        subTitle = self.dateArray[indexPath.row];
        cell.postBtn.enabled = YES;
    }
    [cell setData:@{kimgStr          : imageName,
                    ktitleStr        : title,
                    kdetaiTitleStr   : subTitle}];
    
    if (self.showType == 1) {
        // 对应的日期
        NSDate *date = [NSDate dateFromShortString:self.dateArray[indexPath.row]];
        
        NSString *dayStartStr = [NSString stringWithFormat:@"%@ 00:00:00",[[NSString stringFromDate:date] substringToIndex:10]];
        NSString *dayEndStr = [NSString stringWithFormat:@"%@ 00:00:00",[[NSString stringFromDate:[NSDate dateWithTimeInterval:(24*3600) sinceDate:date]] substringToIndex:10]];
        
        NSString *condition = [NSString stringWithFormat:@"%@ > datetime('%@') and %@ < datetime('%@')",kMeasureDate, dayStartStr, kMeasureDate, dayEndStr];
        NSArray *array = [[DBManage sharedManager] queryDataFromTable:kYunTemperatureTable andConditions:condition];
        BodyTemperatureObj *first = array.firstObject;
        if (first && first.isDownloaded) {
            cell.postBtn.enabled = NO;
        } else {
            cell.postBtn.enabled = YES;
        }
    }
    
    // 正常/禁用状态背景色
    if (cell.postBtn.enabled) {
        cell.postBtn.backgroundColor = kBlueColor;
    } else {
        cell.postBtn.backgroundColor = [UIColor lightGrayColor];
    }
    
    cell.tag = 1000 + indexPath.row;
    
    
    // 按钮回调
    cell.block = ^(HistoryCell *serviceCell){
        
        if (self.showType == 0) {
            // 上传数据
            [self uploadData:self.historyArray[indexPath.row]];
            
        } else {
            
            // 下载数据
            
            MBProgressHUD *hud = [MBProgressHUD showWhileExecuting:@"正在下载..."];
            NSOperationQueue *downloadQueue = [[NSOperationQueue alloc] init];
            NSBlockOperation *opn = [NSBlockOperation blockOperationWithBlock:^{
                [self downloadHistoryDataWithDate:self.dateArray[indexPath.row]];
            }];
            opn.completionBlock = ^{
                kDebug(@"下载数据数 %d", (int)self.yunHistoryArray.count);
                
                NSOperationQueue *queue = [[NSOperationQueue alloc] init];
                NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
                    // 下载该天所有温度数据
                    [self downloadHistoryDataWithDate:self.dateArray[indexPath.row]];
                }];
                blockOp.completionBlock = ^{
                       dispatch_async(dispatch_get_main_queue(), ^{
                        serviceCell.postBtn.enabled = NO;
                        [self.historyTable reloadData];
                    });
                    [hud hide:YES];
                    [MBProgressHUD showSuccess:@"下载完成"];
                };
                [queue addOperation:blockOp];
 
            };
            [downloadQueue addOperation:opn];
            
        }
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 4;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    DataDetailViewController *detail = [[DataDetailViewController alloc] initWithNibName:NSStringFromClass([DataDetailViewController class]) bundle:[NSBundle mainBundle]];
    detail.hidesBottomBarWhenPushed = YES;
    
    if (self.showType == 0) {
        detail.dataArrray = self.historyArray[indexPath.row];
        [self.navigationController pushViewController:detail animated:YES];
    } else {
        
        NSLog(@"");
        //
        //        if (self.yunHistoryArray.count>0) {
        //            detail.dataArrray = self.yunHistoryArray[indexPath.row];
        //        } else {
        //            [MBProgressHUD showError:@"请先下载数据"];
        //            return;
        //        }
    }
    
    
    
}


@end
